// extension的Tab页
var excel = Vue.component('excel', {
    template: readComponentHTML("excel"),
    data() {
        return {
            ruleForm: {
                jsCode: '',
                file: null,
            },
            resp:'',
            fileList: [],
            jsCodeEditor: null,
        }
    },
    mounted() {
        this.setCopyElem(".btn");
        this.jsCodeEditor = this.initCodeEdit("jsCode", "230px", "text/javascript", true);
        this.jsCodeEditor.setValue("function main(excelData){\n" +
            "\treturn appendField(excelData, \"类型\");\n" +
            "}")
    },
    methods: {
        getCodeEditOptions(mode, customTips) {
            var options = {
                lineNumbers: false, // 是否开启行号
                indentWithTabs: true,
                styleActiveLine: true, // 选中行高亮
                mode: mode,
                matchBrackets: true,
                tabSize: 2,
                hintOptions: {
                    completeSingle: false,
                    // hint: this.handleShowHint,
                },
            };
            if (customTips) {
                options.hintOptions["hint"] =  this.handleShowHint;
            }
            return options;
        },
        initCodeEdit(id, height, mode, customTips, readOnly) {
            var editor = CodeMirror.fromTextArea(document.getElementById(id), this.getCodeEditOptions(mode, customTips));
            editor.setSize('auto',height);
            // 代码提示功能 当用户有输入时，显示提示信息
            editor.on("inputRead", (cm) => {
                cm.showHint();
            });
            return editor;
        },
        httpRequest(option) {
            this.fileList.push(option)
        },
        // 上传前处理
        beforeUpload(file) {
            //判断文件类型，必须是xlsx格式
            let fileName = file.name
            return true
        },
        // 文件数量过多时提醒
        handleExceed() {
            this.$message({ type: 'error', message: '最多支持1个附件上传' })
        },
        //导入Excel病种信息数据
        submitImportForm() {
            // 使用form表单的数据格式
            const params = new FormData()
            // 将上传文件数组依次添加到参数paramsData中
            this.fileList.forEach((x) => {
                params.append('file', x.file)
            });
            // 将输入表单数据添加到params表单中
            params.append('jsCode', this.jsCodeEditor.getValue())
            var that = this;
            axios.post('/util/excel', params).then(function (resp) {
                that.resp = resp.data;
            });
        },
        handleRemove() {
            this.$refs.upload.clearFiles()//清空上传列表
            this.fileList = []//集合清空
        },
        handleShowHint(cmInstance, hintOptions) {
            var that = this;
            let cursor = cmInstance.getCursor();
            let cursorLine = cmInstance.getLine(cursor.line);
            let end = cursor.ch;
            let start = end;
            let token = cmInstance.getTokenAt(cursor);
            var tips = [{
                text: "appendField( , )",
                displayText: "appendField",
                displayInfo: "将数组中某个字段拼接起来"
            }]
            tips = tips.filter((data) => {
                data["render"] = that.hintRender;
                var text = data.displayText.toLowerCase();
                var search = token.string.toLowerCase();
                return text.indexOf(search) != -1
            })
            console.log(cmInstance, cursor + "123", cursorLine, end, token);
            return {
                list: tips,
                from: {
                    ch: token.start,
                    line: cursor.line
                },
                to: {
                    ch: token.end,
                    line: cursor.line
                }
            };
        }
    }
})